<?php

namespace app\company\model;

use Exception;

/**
 *  门店用户表
 * @property int $id
 * @property int $store_id 门店id
 * @property  string $password 密码
 *  @property  int $company_id  公司id
 */
class StoreUser extends Base
{

    public static function onBeforeInsert(StoreUser $model): void
    {
        $model->password = md5('123456' . 'dt');
        $model->company_id = getCompanyId();
    }

    public static function onAfterInsert(StoreUser $model): void
    {
        StoreUserAccount::create(['store_user_id' => $model->id]);//关联创建账户
        $param = request()->param();
        //关联门店
        if(request()->param('store_ids', '')){
            $store_ids = explode(',', $param['store_ids']);
            //出现一对多关系;
            foreach ($store_ids as $store_id){
                StoreSalesman::create(['store_id' => $store_id, 'store_user_id' => $model->id]);
            }
//            Store::whereIn('id', $store_ids)->update(['salesman_id' => $model->id]);
        }
    }

    /**
     * @param StoreUser $model
     * @return void
     * @throws Exception
     */
    public static function onBeforeUpdate(StoreUser $model): void
    {
        $param = request()->param();
        if(request()->param('password', '')){
            $model->password = md5($param['password'] . 'dt');
            return;
        }
        //修改账户
        if(request()->param('money', 0)) {
            $type = request()->param('type', ''); //操作类型 5:后台增加 6:后台减少
            if(!in_array($type, ['5', '6'])) exception( '金额操作类型错误');
            //查询出账户
            $account = StoreUserAccount::where('store_user_id', $model->id)->find();
            // $account->inc('money',$param['money'])->update();
            $account->inc('money',$param['money'])->save();
            //添加动账记录
            $arr['store_user_id'] = $model->id;
            $arr['store_user_account_id'] = $account->id;
            $arr['money'] = $param['money'];
            $arr['type'] = $type;
            $arr['company_user_id'] = getCompanyUserId();
            StoreUserAccountDetail::create($arr);
        }

        //修改关联门店
        if(request()->param('store_ids', '')){
            $store_ids = explode(',', $param['store_ids']);
//            Store::where('salesman_id',  $model->id)->update(['salesman_id' => 0]);//解除绑定
//            Store::whereIn('id', $store_ids)->update(['salesman_id' => $model->id]);//添加新的绑定
            // 删除绑定的门店
            StoreSalesman::destroy(function ($query) use ($model){
                $query->where('store_user_id', $model->id);
            },true);
            // 重新绑定
            foreach ($store_ids as $store_id){
                StoreSalesman::create(['store_id' => $store_id, 'store_user_id' => $model->id]);
            }
        }else{
            // 有点搞笑，两个接口共用一个方法，不多传参数或者别的办法限制，一棍子敲死，不是A就是B，咋想的
            // 增加一个额外参数，如果是修改密码进来的就不走下面的修改门店，或者在修改密码那块return
            // if(!request()->param('mp', false)){
                
            // }
            // 删除绑定的门店
            StoreSalesman::destroy(function ($query) use ($model){
                $query->where('store_user_id', $model->id);
            },true);
        }
    }
}